package com.ruoyi.web.controller.wms;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wms.domain.WmsWarehouseArea;
import com.ruoyi.wms.service.IWmsWarehouseAreaService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 区域信息Controller
 * 
 * @author ruoyi
 * @date 2024-12-19
 */
@RestController
@RequestMapping("/wms/area")
public class WmsAreaController extends BaseController
{
    @Autowired
    private IWmsWarehouseAreaService wmsWarehouseAreaService;

    /**
     * 查询区域信息列表
     */
    @PreAuthorize("@ss.hasPermi('wms:area:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsWarehouseArea wmsWarehouseArea)
    {
        startPage();
        List<WmsWarehouseArea> list = wmsWarehouseAreaService.selectWmsWarehouseAreaList(wmsWarehouseArea);
        return getDataTable(list);
    }

    /**
     * 导出区域信息列表
     */
    @PreAuthorize("@ss.hasPermi('wms:area:export')")
    @Log(title = "区域信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsWarehouseArea wmsWarehouseArea)
    {
        List<WmsWarehouseArea> list = wmsWarehouseAreaService.selectWmsWarehouseAreaList(wmsWarehouseArea);
        ExcelUtil<WmsWarehouseArea> util = new ExcelUtil<WmsWarehouseArea>(WmsWarehouseArea.class);
        util.exportExcel(response, list, "区域信息数据");
    }

    /**
     * 获取区域信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:area:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wmsWarehouseAreaService.selectWmsWarehouseAreaById(id));
    }

    /**
     * 新增区域信息
     */
    @PreAuthorize("@ss.hasPermi('wms:area:add')")
    @Log(title = "区域信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsWarehouseArea wmsWarehouseArea)
    {
        if (!wmsWarehouseAreaService.checkAreaCodeUnique(wmsWarehouseArea))
        {
            return error("新增区域'" + wmsWarehouseArea.getAreaName() + "'失败，区域编码已存在");
        }
        wmsWarehouseArea.setCreateBy(getUsername());
        return toAjax(wmsWarehouseAreaService.insertWmsWarehouseArea(wmsWarehouseArea));
    }

    /**
     * 修改区域信息
     */
    @PreAuthorize("@ss.hasPermi('wms:area:edit')")
    @Log(title = "区域信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsWarehouseArea wmsWarehouseArea)
    {
        if (!wmsWarehouseAreaService.checkAreaCodeUnique(wmsWarehouseArea))
        {
            return error("修改区域'" + wmsWarehouseArea.getAreaName() + "'失败，区域编码已存在");
        }
        wmsWarehouseArea.setUpdateBy(getUsername());
        return toAjax(wmsWarehouseAreaService.updateWmsWarehouseArea(wmsWarehouseArea));
    }

    /**
     * 根据仓库ID查询区域列表
     */
    @PreAuthorize("@ss.hasPermi('wms:area:list')")
    @GetMapping("/warehouse/{warehouseId}")
    public AjaxResult getAreasByWarehouseId(@PathVariable("warehouseId") Long warehouseId)
    {
        WmsWarehouseArea queryArea = new WmsWarehouseArea();
        queryArea.setWarehouseId(warehouseId);
        queryArea.setStatus(0); // 只查询启用状态的区域
        List<WmsWarehouseArea> list = wmsWarehouseAreaService.selectWmsWarehouseAreaList(queryArea);
        return success(list);
    }

    /**
     * 删除区域信息
     */
    @PreAuthorize("@ss.hasPermi('wms:area:remove')")
    @Log(title = "区域信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wmsWarehouseAreaService.deleteWmsWarehouseAreaByIds(ids));
    }
}